package com.interauto.Controller.casepage;

import com.interauto.bean.web.AssinfoWeb;
import com.interauto.bean.web.CaseinfoWeb;
import com.interauto.bean.web.InterinfoWeb;
import com.interauto.service.AutoDebugRunInterService;
import com.interauto.service.web.CasePageService;
import com.interauto.service.web.InterinfoPageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.interauto.bean.autoenum.AutoExpectEnum.getAutoExpectMap;
import static com.interauto.bean.autoenum.HttpEnum.HttpMeth.getHttpMethMap;
import static com.interauto.bean.autoenum.HttpEnum.HttpType.getHttpTypeMap;

/**
 * 用例页面相关哈
 */
@Controller
@RequestMapping(value = "/case")
public class CaseHome {
    private static final Logger logger= LoggerFactory.getLogger(CaseHome.class);
    @Autowired
    private CasePageService casePageService;
    @Autowired
    private InterinfoPageService interinfoPageService;
    @Autowired
    private AutoDebugRunInterService autoDebugRunInterService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ModelAndView caselistpage(@RequestParam(value = "projectid") Integer projectid){
        List<CaseinfoWeb> caseinfoWebs=casePageService.getCaseinfoList(projectid);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("projectid",projectid);
        modelAndView.addObject("caselists",caseinfoWebs);
        modelAndView.setViewName("caselistpage");
        return modelAndView;
    }

    @RequestMapping(value = "/caseinfo",method = RequestMethod.GET)
    public ModelAndView interlistpage(@RequestParam(value = "caseid") Integer caseid,
                                      @RequestParam(value = "interinfoid",required=false) Integer interinfoid){
        ModelAndView modelAndView=new ModelAndView();
        CaseinfoWeb caseinfoWeb= casePageService.getCaseinfo(caseid);
        modelAndView.addObject("caseinfo",caseinfoWeb);
        modelAndView.addObject("httpmethmap",getHttpMethMap());
        modelAndView.addObject("httptypemap",getHttpTypeMap());
        modelAndView.addObject("autoexpectmap",getAutoExpectMap());
        List<InterinfoWeb> interinfoWebs=interinfoPageService.getInterinfoforCaseid(caseid);  //查询用例关联的所有接口信息
        modelAndView.addObject("interinfoWebs",interinfoWebs);
        int sinterinfoid=-1;
        if(interinfoid!=null && interinfoid>0){
            InterinfoWeb interinfoWeb=interinfoPageService.getIngerinfforid(interinfoid);
            setinithttpinfo(interinfoWeb);
            modelAndView.addObject("interinfoWeb",interinfoWeb);
            sinterinfoid=interinfoid;
            modelAndView.addObject("getinterinfoid",sinterinfoid);
            modelAndView.setViewName("editinterinfopage");
            return modelAndView;
        }
        modelAndView.addObject("getinterinfoid",sinterinfoid);
        modelAndView.setViewName("interinfopage");
        return modelAndView;
    }

    @RequestMapping(value = "/debuginter",method = RequestMethod.POST)
    @ResponseBody
    public Object debuginterweb(@RequestBody InterinfoWeb interinfoWeb){
        return autoDebugRunInterService.debugruninterinfo(interinfoWeb);
    }


    /**
     * 提供给编辑用的，初始化header输入框和断言的输入框
     * @param interinfoWeb 要初始化的接口信息
     */
    private void setinithttpinfo(InterinfoWeb interinfoWeb){
        if(interinfoWeb == null){
            return;
        }
        Map<String,String> headmap=interinfoWeb.getHeadstr();
        if(headmap == null || headmap.isEmpty()){
            Map<String,String> newheadermap=new HashMap<>();
            newheadermap.put("","");
            interinfoWeb.setHeadstr(newheadermap);
        }
        List<AssinfoWeb> assinfoWebs=interinfoWeb.getAssinfoWebs();
        if(assinfoWebs == null || assinfoWebs.isEmpty()){
            List<AssinfoWeb> assinfoWebList=new ArrayList<>();
            assinfoWebList.add(new AssinfoWeb("","",1));
            interinfoWeb.setAssinfoWebs(assinfoWebList);
        }
    }

}
